package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.dto.TalentPoolQueryDTO;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.TalentPool;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.cms.service.TalentPoolService;
import com.jeff.tianti.common.ColumnInfoCom;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/channel/talent")//江苏频道_人才库
public class TalentPoolController {
    @Autowired
    private TalentPoolService talentPoolService;
    @Autowired
    private ColumnInfoCom columnInfoCom;

    /**
     * @Ceator:liuYI
     * @Describe:人才库列表数据展示
     */
    @RequestMapping("/pool/list")
    public String findAll(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String talentType = request.getParameter("talentType");//cms_column_info外键   人才类型，后台可配置  列:行业专家，精英人才
        String type = request.getParameter("type");//cms_column_info外键   学科类型，在cms_column_info中为人才类型的子列表，   例:学术类，实务类，管理类。。。
        String deleteFlag = request.getParameter("deleteFlag");//删除标记0->正常 1->删除
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        Map map0 = columnInfoCom.findColumnInfoList(Constants.COLUMNINFO_RCQ_CODE);//父
        Map map1 = new HashMap<>();
        if (StringUtils.isNotBlank(talentType)) {//根据对应的父获取对应的子
            map1 = columnInfoCom.findColumnInfoList(talentType);
        }
        TalentPoolQueryDTO talentPoolQueryDTO = new TalentPoolQueryDTO();
        talentPoolQueryDTO.setCurrentPage(currentPage);
        talentPoolQueryDTO.setPageSize(pageSize);
        talentPoolQueryDTO.setType(type);
        talentPoolQueryDTO.setTalentType(talentType);
        talentPoolQueryDTO.setDeleteFlag(deleteFlag);
        PageModel<TalentPool> page = talentPoolService.queryTalentPoolPage(talentPoolQueryDTO);
        model.addAttribute("dto", talentPoolQueryDTO);
        model.addAttribute("map0", map0);
        model.addAttribute("map1", map1);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute(Constants.MENU_NAME, Constants.TALENT_POOL);
        model.addAttribute("page", page);
        return "/channel/talentPool/list";
    }

    @Autowired
    private ColumnInfoService columnInfoService;

    /**
     * @Ceator:liuYI
     * @Describe:人才库详情页数据
     */
    @RequestMapping("/pool/edit")
    public String articleEdit(HttpServletRequest request, Model model) {
        TalentPool talentPool = null;
        String id = request.getParameter("id");
        String rootColumnId = request.getParameter("rootColumnId");
        String columnId = request.getParameter("columnId");
        if (StringUtils.isNotBlank(id)) {
            talentPool = talentPoolService.find(id);
            if (talentPool != null) {
                rootColumnId = talentPool.getTalentType();
                columnId = talentPool.getType();
            }
        }
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setLevel(2);
        columnInfoQueryDTO.setCode("jspd_rck");
        columnInfoQueryDTO.setIsCodeLike(true);
        List<ColumnInfo> rootCoulumnInfoList = this.columnInfoService.queryColumnInfoList(columnInfoQueryDTO);

        model.addAttribute("rootColumnId", rootColumnId);
        model.addAttribute("columnId", columnId);
        model.addAttribute("talentPool", talentPool);
        model.addAttribute("rootCoulumnInfoList", rootCoulumnInfoList);
        model.addAttribute(Constants.MENU_NAME, Constants.TALENT_POOL);
        return "/channel/talentPool/edit";
    }

    /**
     * @Ceator:liuYI
     * @Describe:人才库数据添加操作
     */
    @RequestMapping("/pool/save")
    @ResponseBody
    public AjaxResult save(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String talentType = request.getParameter("rootColumnId");
            String type = request.getParameter("leafColumnId");
            String url = request.getParameter("url");
            String file = request.getParameter("file");
            String name = request.getParameter("name");
            String content = request.getParameter("content");
            if (StringUtils.isNotBlank(id)) {//修改
                TalentPool talentPool = talentPoolService.find(id);
                talentPool.setContent(content);
                talentPool.setName(name);
                talentPool.setFile(file);
                talentPool.setUrl(url);
                String oldType = talentPool.getType();
                if (!type.equals(oldType)) {
                    talentPool.setType(type);
                    ColumnInfo columnInfo = columnInfoService.find(type);
                    talentPool.setTypeName(columnInfo.getName());
                }
                String oldTalentType = talentPool.getTalentType();
                if (!talentType.equals(oldTalentType)) {
                    talentPool.setTalentType(talentType);
                    ColumnInfo columnInfo = columnInfoService.find(talentType);
                    talentPool.setTalentTypeName(columnInfo.getName());
                }
                talentPoolService.update(talentPool);
            } else {//添加
                TalentPool talentPool = new TalentPool();
                talentPool.setContent(content);
                talentPool.setName(name);
                talentPool.setFile(file);
                talentPool.setUrl(url);
                ColumnInfo columnInfo = columnInfoService.find(type);
                talentPool.setType(type);
                talentPool.setTypeName(columnInfo.getName());
                ColumnInfo columnInfo1 = columnInfoService.find(talentType);
                talentPool.setTalentType(talentType);
                talentPool.setTalentTypeName(columnInfo1.getName());
                talentPool.setDeleteFlag(ColumnInfo.DELETE_FLAG_NORMAL);
                talentPool.setCreateDate(new Date());
                talentPoolService.save(talentPool);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     * @Ceator:liuYI
     * @Describe:用户删除
     */
    @RequestMapping("/pool/del")
    @ResponseBody
    public AjaxResult delete(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            if (StringUtils.isNotBlank(id)) {
                TalentPool talentPool = talentPoolService.find(id);
                String deleteFlag = talentPool.getDeleteFlag();
                deleteFlag = ColumnInfo.DELETE_FLAG_DELETED.equals(deleteFlag) ? ColumnInfo.DELETE_FLAG_NORMAL : ColumnInfo.DELETE_FLAG_DELETED;
                talentPool.setDeleteFlag(deleteFlag);
                talentPoolService.update(talentPool);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     * @author:liuyi
     * @date:2020-01-15 10:44:37
     * @describe:置顶功能
     */
    @ResponseBody
    @RequestMapping(value = "/stick/{id}", method = RequestMethod.GET)
    public AjaxResult stick(@PathVariable String id) {
        TalentPool talentPool = talentPoolService.findMaxSort();
        TalentPool result = talentPoolService.find(id);
        int sort = talentPool.getSort();
        sort++;
        result.setSort(sort);
        talentPoolService.update(result);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }
}
